package com.be.beadmin.device.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.dto.output.MaintenanceInfoExcelEx;
import com.be.beadmin.device.entity.MaintenanceInfo;
import com.be.beadmin.device.dto.output.MaintenanceInfoEx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.device.service.MaintenanceInfoService;
import com.be.beadmin.device.dto.input.MaintenanceInfoQueryCriteria;
import com.be.beadmin.device.dto.input.MaintenanceInfoDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author zty
* @date 2023-08-01
**/
@RestController
public class MaintenanceInfoController {

    @Autowired
    private  MaintenanceInfoService maintenanceInfoService;


    @PostMapping(value = "/device/getMaintenanceInfoByPage")
    public ResultModel getMaintenanceInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(maintenanceInfoService.getMaintenanceInfoByPage(pageEntity)) ;
    }

    @PostMapping("/device/addMaintenanceInfo")
    public ResultModel addMaintenanceInfo(@RequestBody MaintenanceInfoDto maintenanceInfoDto){
        maintenanceInfoService.addMaintenanceInfo(maintenanceInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delMaintenanceInfo")
    public ResultModel delMaintenanceInfo(@RequestBody MaintenanceInfoDto maintenanceInfoDto){
        maintenanceInfoService.delMaintenanceInfo(maintenanceInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateMaintenanceInfo")
    public ResultModel updateMaintenanceInfo(@RequestBody MaintenanceInfoDto maintenanceInfoDto){
        maintenanceInfoService.updateMaintenanceInfo(maintenanceInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @PostMapping("/device/exportMaintenanceInfo")
    public void exportMaintenanceInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<MaintenanceInfoEx> maintenanceInfoPage = maintenanceInfoService.getMaintenanceInfoByPage(pageEntity);
        List<MaintenanceInfoEx> maintenanceInfoExes = maintenanceInfoPage.getContent();
        List<MaintenanceInfoExcelEx> maintenanceInfoExcelExes=maintenanceInfoExes.stream().map(item->{
            MaintenanceInfoExcelEx excel=new MaintenanceInfoExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("MaintenanceInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), MaintenanceInfoExcelEx.class).sheet("sheet1").doWrite(maintenanceInfoExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), MaintenanceInfoExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(maintenanceInfoExcelExes);
    }

    @ApiOperation("根据唯一标识获取维修工单")
    @PostMapping("/device/getMaintenanceInfoByIdentification")
    public ResultModel getMaintenanceInfoByIdentification(@RequestParam String identification){
        return ResultModel.success(maintenanceInfoService.getMaintenanceInfoByIdentification(identification)) ;
    }


    @ApiOperation("根据创建人姓名获取维修工单")
    @PostMapping("/device/getMaintenanceInfoByCreateName")
    public ResultModel getMaintenanceInfoByCreateName(@RequestParam String createName){
            return ResultModel.success(maintenanceInfoService.getMaintenanceInfoByCreateName(createName)) ;
    }

    @ApiOperation("根据指派人员获取维修工单")
    @PostMapping("/device/getMaintenanceInfoByAssignedTo")
    public ResultModel getMaintenanceInfoByAssignedTo(@RequestParam String assignedTo){
        return ResultModel.success(maintenanceInfoService.getMaintenanceInfoByAssignedTo(assignedTo)) ;
    }

    @ApiOperation("根据指派人员和完成状态获取维修工单")
    @PostMapping("/device/getMaintenanceInfoByAssignedToAndState")
    public ResultModel getMaintenanceInfoByAssignedToAndState(@RequestParam String assignedTo, @RequestParam String state){
        return ResultModel.success(maintenanceInfoService.getMaintenanceInfoByAssignedToAndState(assignedTo, state)) ;
    }

    @ApiOperation("获取维修专员列表")
    @PostMapping("/device/getMaintenanceCommissioners")
    public ResultModel getMaintenanceCommissioners(){
        return ResultModel.success(maintenanceInfoService.getMaintenanceCommissioners());
    }

    @ApiOperation("计算维修工时")
    @PostMapping("/device/getMaintenanceHours")
    public ResultModel getMaintenanceHours(@RequestBody MaintenanceInfo maintenanceInfo){
        Long hours = maintenanceInfoService.getMaintenanceHours(maintenanceInfo);
        return ResultModel.success(hours);
    }

    @ApiOperation("自动生成维保任务")
    @PostMapping("/device/generateMaintenanceInfoByInspect")
    public ResultModel generateMaintenanceInfoByInspect(String faultDesc){
        maintenanceInfoService.generateMaintenanceInfoByInspect(faultDesc);
        return ResultModel.success();
    }
}